<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="phonenumber">
        <el-input v-model="queryParams.phonenumber" placeholder="请输入手机号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入用户名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="查询时间">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="YYYY-MM-DD" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="交易状态" prop="payStatus">
        <el-select @change="getList" v-model="queryParams.payStatus" placeholder="请选择交易状态" clearable>
          <el-option v-for="dict in pay_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="支付渠道" prop="payChannel">
        <el-select @change="getList" v-model="queryParams.payChannel" placeholder="请选择支付渠道" clearable>
          <el-option v-for="dict in pay_channel" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList">
      <el-table-column label="订单号" align="center" prop="orderNo" :show-overflow-tooltip="true" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="手机号" align="center" prop="phonenumber" />
      <el-table-column label="交易状态" align="center" prop="payStatus">
        <template #default="scope">
          <dict-tag :options="pay_status" :value="scope.row.payStatus" />
        </template>
      </el-table-column>
      <el-table-column label="订单金额" align="center" prop="totalAmount">
        <template #default="scope">
          <span>￥{{ scope.row.totalAmount / 100 || 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付积分" align="center" prop="payerCoins">
        <template #default="scope">
          <span>{{ scope.row.payerCoins / 100 || 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实付金额" align="center" prop="payerTotal">
        <template #default="scope">
          <span>￥{{ scope.row.payerTotal / 100 || 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="已退款金额" align="center" prop="payerRefundTotal">
        <template #default="scope">
          <span>￥{{ scope.row.payerRefundTotal / 100 || 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="已退款积分" align="center" prop="payerRefundCoinsTotal">
        <template #default="scope">
          <span>{{ scope.row.payerRefundCoinsTotal }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付渠道" align="center" prop="payChannel">
        <template #default="scope">
          <dict-tag v-if="scope.row.payChannel" :options="pay_channel" :value="scope.row.payChannel" />
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary"
            v-if="(scope.row.payStatus == 'SUCCESS' || scope.row.payStatus == 'REFUND') && (scope.row.payerTotal > scope.row.payerRefundTotal)"
            @click="handleRefund(scope.row)">退款
          </el-button>
          <span v-else>-</span>
        </template>
      </el-table-column>
    </el-table>

    <DialogRefund ref="refundRef" :reload="getList" />

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script setup name="RechargeOrder">
import { listRechargeOrder } from "@/api/trade/order";
import DialogRefund from "@/dialog/order/DialogRefund";

const route = useRoute();
const router = useRouter();
const { proxy } = getCurrentInstance();
const { pay_status, pay_channel } = proxy.useDict("pay_status", "pay_channel");

const loading = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const showSearch = ref(true);
const total = ref(0);
const infoList = ref([])
const dateRange = ref("")

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  chargeMode: null,
  chargeStatus: null,
  chargeType: null,
  payStatus: null,
  endTime: null,
  beginTime: null,
})

// 查询订单信息列表
function getList() {
  loading.value = true;
  if (queryParams.value.chargeMode == "") queryParams.value.chargeMode = null;
  if (queryParams.value.chargeStatus == "")
    queryParams.value.chargeStatus = null;
  if (queryParams.value.chargeType == "") queryParams.value.chargeType = null;
  if (queryParams.value.payStatus == "") queryParams.value.payStatus = null;
  listRechargeOrder(proxy.addDateRange(queryParams.value, dateRange.value)).then((response) => {
    infoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 搜索按钮操作
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

// 重置按钮操作
function resetQuery() {
  proxy.resetForm("queryForm");
  handleQuery();
}

// 退款处理
function handleRefund(row) {
  proxy.$refs["refundRef"].open(row);
}

const beginTime = route.query.beginTime;
const endTime = route.query.endTime;
if (beginTime != undefined && endTime != undefined) {
  dateRange.value = [
    decodeURIComponent(beginTime),
    decodeURIComponent(endTime),
  ];
}
getList();

</script>
